﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;

namespace DACK_MHTTUD_0912008_0912493
{
    public partial class UCHash : UserControl
    {
        public UCHash()
        {
            InitializeComponent();
        }

        string strFile = "";
        string text = "";
        string result = "";
        string strHashData = "";
        List<string> dataFormat = new List<string>();
        int index = 0;

        private void Init()
        {
            dataFormat.Add("Text");
            dataFormat.Add("File");
            comboBoxDataFormat.DataSource = dataFormat;

            comboBoxDataFormat.SelectedIndex = index;
            btnBrowse.Visible = false;
        }

        private void UCHash_Load(object sender, EventArgs e)
        {
            Init();
        }

        private void comboBoxDataFormat_SelectedIndexChanged(object sender, EventArgs e)
        {
           index = comboBoxDataFormat.SelectedIndex;

            switch (index)
            {
                case 0:
                    btnBrowse.Visible = false;
                    text = txtData.Text;
                    break;
                case 1:
                    btnBrowse.Visible = true;
                    strFile = txtData.Text;
                    break;
            }
        }

        private bool check()
        {
            if (txtData.Text == "")
            {
                return false;
            }
            return true;
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            txtMD5.Text = "";
            txtSHA1.Text = "";
            txtSHA256.Text = "";
            txtSHA384.Text = "";
            txtSHA512.Text = "";
            if (check())
            {
                try
                {
                    if (checkBoxMD5.Checked)
                    {
                        txtMD5.Text = GetMD5Hash(txtData.Text);
                    }
                    if (checkBoxSHA256.Checked)
                    {
                        txtSHA256.Text = GetSHA256Hash(txtData.Text);
                    }
                    if (checkBoxSHA384.Checked)
                    {
                        txtSHA384.Text = GetSHA384Hash(txtData.Text);
                    }
                    if (checkBoxSHA512.Checked)
                    {
                        txtSHA512.Text = GetSHA512Hash(txtData.Text);
                    }
                    if (SHA1.Checked)
                    {
                        txtSHA1.Text = GetSHA1Hash(txtData.Text);
                    }
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
               
            }
            else
            {
                MessageBox.Show("Please input data!", "Error", MessageBoxButtons.OK);
            }
        }

        private string GetMD5Hash(string value)
        {
            byte[] arrbyteHashValue = null;
            FileStream inputFileStream = null;

            MD5CryptoServiceProvider MD5Hasher = new MD5CryptoServiceProvider();
            //SHA256CryptoServiceProvider SHA
            switch (index)
            {
                case 0:
                    byte[] input = Encoding.ASCII.GetBytes(value);
                    arrbyteHashValue = MD5Hasher.ComputeHash(input);
                    break;
                case 1:
                    //try
                    //{
                    inputFileStream = new FileStream(value, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    arrbyteHashValue = MD5Hasher.ComputeHash(inputFileStream);
                    inputFileStream.Close();

                    //}
                    //catch (Exception ex)
                    //{
                    //    MessageBox.Show(ex.Message);
                    //}
                    break;
            }

            strHashData = System.BitConverter.ToString(arrbyteHashValue);
            strHashData = strHashData.Replace("-", "");
            result = strHashData;

            return result;
        }

        private string GetSHA512Hash(string value)
        {
            byte[] arrbyteHashValue = null;
            FileStream inputFileStream = null;

            SHA512CryptoServiceProvider SHA512Hasher = new SHA512CryptoServiceProvider();
            //SHA256CryptoServiceProvider SHA
            switch (index)
            {
                case 0:
                    byte[] input = Encoding.ASCII.GetBytes(value);
                    arrbyteHashValue = SHA512Hasher.ComputeHash(input);
                    break;
                case 1:
                    //try
                    //{
                    inputFileStream = new FileStream(value, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    arrbyteHashValue = SHA512Hasher.ComputeHash(inputFileStream);
                    inputFileStream.Close();

                    //}
                    //catch (Exception ex)
                    //{
                    //    MessageBox.Show(ex.Message);
                    //}
                    break;
            }

            strHashData = System.BitConverter.ToString(arrbyteHashValue);
            strHashData = strHashData.Replace("-", "");
            result = strHashData;

            return result;
        }

        private string GetSHA384Hash(string value)
        {
            byte[] arrbyteHashValue = null;
            FileStream inputFileStream = null;

            SHA384CryptoServiceProvider SHA384Hasher = new SHA384CryptoServiceProvider();
            //SHA256CryptoServiceProvider SHA
            switch (index)
            {
                case 0:
                    byte[] input = Encoding.ASCII.GetBytes(value);
                    arrbyteHashValue = SHA384Hasher.ComputeHash(input);
                    break;
                case 1:
                    //try
                    //{
                    inputFileStream = new FileStream(value, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    arrbyteHashValue = SHA384Hasher.ComputeHash(inputFileStream);
                    inputFileStream.Close();

                    //}
                    //catch (Exception ex)
                    //{
                    //    MessageBox.Show(ex.Message);
                    //}
                    break;
            }

            strHashData = System.BitConverter.ToString(arrbyteHashValue);
            strHashData = strHashData.Replace("-", "");
            result = strHashData;

            return result;
        }

        private string GetSHA256Hash(string value)
        {
            byte[] arrbyteHashValue = null;
            FileStream inputFileStream = null;

            SHA256CryptoServiceProvider SHA256Hasher = new SHA256CryptoServiceProvider();
            //SHA256CryptoServiceProvider SHA
            switch (index)
            {
                case 0:
                    byte[] input = Encoding.ASCII.GetBytes(value);
                    arrbyteHashValue = SHA256Hasher.ComputeHash(input);
                    break;
                case 1:
                    //try
                    //{
                    inputFileStream = new FileStream(value, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    arrbyteHashValue = SHA256Hasher.ComputeHash(inputFileStream);
                    inputFileStream.Close();

                    //}
                    //catch (Exception ex)
                    //{
                    //    MessageBox.Show(ex.Message);
                    //}
                    break;
            }

            strHashData = System.BitConverter.ToString(arrbyteHashValue);
            strHashData = strHashData.Replace("-", "");
            result = strHashData;

            return result;
        }

        private string GetSHA1Hash(string value)
        {
            byte[] arrbyteHashValue = null;
            FileStream inputFileStream = null;

            SHA1CryptoServiceProvider SHA1Hasher = new SHA1CryptoServiceProvider();
            //SHA256CryptoServiceProvider SHA
            switch (index)
            {
                case 0 :
                    byte[] input = Encoding.ASCII.GetBytes(value);
                    arrbyteHashValue = SHA1Hasher.ComputeHash(input);
                    break;
                case 1:
                    //try
                    //{
                        inputFileStream = new FileStream(value, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                        arrbyteHashValue = SHA1Hasher.ComputeHash(inputFileStream);
                        inputFileStream.Close();
                        
                    //}
                    //catch (Exception ex)
                    //{
                    //    MessageBox.Show(ex.Message);
                    //}
                    break;
            }

            strHashData = System.BitConverter.ToString(arrbyteHashValue);
            strHashData = strHashData.Replace("-", "");
            result = strHashData;

            return result;
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.ShowDialog();
            txtData.Text = openFile.FileName;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }
    }
}
